package com.abe.libcore.utils.normal;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DateUtils {

    public enum QuickKey {
        DAY, WEEK, MONTH, QUARTER, YEAR
    }

    public static String[] QuickDateCreator(QuickKey key) {
        String[] times = new String[2];
        switch (key) {
            case DAY:
                times = QuickDateCreator("本日");
                break;
            case WEEK:
                times = QuickDateCreator("本周");
                break;
            case MONTH:
                times = QuickDateCreator("本月");
                break;
            case QUARTER:
                times = QuickDateCreator("本季度");
                break;
            case YEAR:
                times = QuickDateCreator("本年");
                break;
            default:
                times = QuickDateCreator("本日");
                break;
        }
        return times;
    }

    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     *
     * @return true <br/>false
     */
    public static boolean isDateOneBigger(Date dt1, Date dt2) {
        boolean isBigger = false;
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = true;
        } else if (dt1.getTime() < dt2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }

    /**
     * 根据快捷名称生成对应起始日期
     */
    public static String[] QuickDateCreator(String QuickDateName) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String[] dates = new String[2];
        dates[1] = dateFormat.format(new Date());
        switch (QuickDateName) {
            case "本日":
                dates[0] = dates[1];
                break;
            case "本周":
                String weekDay = new SimpleDateFormat("E", Locale.CHINA).format(new Date());
                switch (weekDay) {
                    case "周一":
                        dates[0] = dates[1];
                        break;
                    case "周二":
                        dates[0] = dateFormat.format(GetDateAgo(1));
                        break;
                    case "周三":
                        dates[0] = dateFormat.format(GetDateAgo(2));
                        break;
                    case "周四":
                        dates[0] = dateFormat.format(GetDateAgo(3));
                        break;
                    case "周五":
                        dates[0] = dateFormat.format(GetDateAgo(4));
                        break;
                    case "周六":
                        dates[0] = dateFormat.format(GetDateAgo(5));
                        break;
                    case "周日":
                        dates[0] = dateFormat.format(GetDateAgo(6));
                        break;
                }
                break;
            case "本月": {
                String year_month = new SimpleDateFormat("yyyy-MM", Locale.CHINA)
                        .format(new Date());
                dates[0] = new StringBuffer().append(year_month).append("-01")
                        .toString();
            }
            break;
            case "本季度": {
                String year = new SimpleDateFormat("yyyy", Locale.CHINA).format(new Date());
                int month = new Date().getMonth();
                if (month >= 1 && month <= 3) {
                    dates[0] = new StringBuffer(year).append("-01-01").toString();
                } else if (month >= 4 && month <= 6) {
                    dates[0] = new StringBuffer(year).append("-04-01").toString();
                } else if (month >= 7 && month <= 9) {
                    dates[0] = new StringBuffer(year).append("-07-01").toString();
                } else {
                    dates[0] = new StringBuffer(year).append("-10-01").toString();
                }
            }
            break;
            case "本年": {
                String year = new SimpleDateFormat("yyyy", Locale.CHINA).format(new Date());
                dates[0] = new StringBuffer(year).append("-01-01").toString();
            }
            break;
        }

        return dates;
    }

    /**
     * 获取days天数差的日期
     */
    public static Date GetDateAgo(int days) {
        long currentTime = System.currentTimeMillis();
        long daysAgoTime = currentTime - days * 24 * 60 * 60 * 1000;
        return new Date(daysAgoTime);
    }

    /**
     * 获取时间段内的日期列表（年月日三种）
     *
     * @param rule  日期格式（yyyy-MM-dd,yyyyMMdd,yyyy-MM,yyyyMM）
     * @param type  年月日:year,month,day
     * @param start 开始时间
     * @param end   截止时间
     */
    public static List<String> GetDateList(String rule, String type, Date start, Date end) {
        List<String> datas = new ArrayList<>();
        if (TextUtils.isEmpty(rule) || TextUtils.isEmpty(type) || start == null || end == null)
            return datas;
        switch (type) {
            case "year": {
                int startYear = getDateInfo(TYPE_YEAR, start);
                int endYear = getDateInfo(TYPE_YEAR, end);
                for (int i = startYear; i < endYear + 1; i++) {
                    datas.add(String.valueOf(i));
                }
            }
            break;
            case "month": {
                int startYear = getDateInfo(TYPE_YEAR, start);
                int endYear = getDateInfo(TYPE_YEAR, end);
                int startMonth = getDateInfo(TYPE_MONTH, start);
                int endMonth = getDateInfo(TYPE_MONTH, end);
                for (int i = startMonth; i <= 12; i++) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(startYear);
                    if (rule.equals("yyyy-MM")) {
                        builder.append("-");
                    }
                    if (i < 10) {
                        builder.append(0);
                    }
                    builder.append(i);
                    datas.add(builder.toString());
                }
                for (int i = startYear + 1; i < endYear; i++) {
                    for (int j = 0; j <= 12; j++) {
                        StringBuilder builder = new StringBuilder();
                        builder.append(i);
                        if (rule.equals("yyyy-MM")) {
                            builder.append("-");
                        }
                        if (j < 10) {
                            builder.append(0);
                        }
                        builder.append(j);
                        datas.add(builder.toString());
                    }
                }
                for (int i = 1; i <= endMonth; i++) {
                    StringBuilder builder = new StringBuilder();
                    builder.append(endYear);
                    if (rule.equals("yyyy-MM")) {
                        builder.append("-");
                    }
                    if (i < 10) {
                        builder.append(0);
                    }
                    builder.append(i);
                    datas.add(builder.toString());
                }
            }
            break;
            case "day": {
                Date tempDate = start;
                while (!DateToString("yyyy-MM-dd", tempDate).equals(DateToString("yyyy-MM-dd", end))) {
                    datas.add(DateToString(rule, tempDate));
                    tempDate = getDiffDate(TYPE_DAY, tempDate, 1);
                }
                datas.add(DateToString(rule, end));
            }
            break;
            default: {
            }
            break;
        }
        return datas;
    }

    /**
     * yyyy
     */
    public static final String TYPE_YEAR = "yyyy";
    /**
     * MM
     */
    public static final String TYPE_MONTH = "MM";
    /**
     * dd
     */
    public static final String TYPE_DAY = "dd";
    /**
     * HH:mm:ss
     */
    public static final String TYPE_Time = "HH:mm:ss";
    /**
     * yyyy-MM
     */
    public static final String TYPE_YEAR_MONTH = "yyyy-MM";
    /**
     * yyyy年MM月
     */
    public static final String TYPE_YEAR_MONTH_1 = "yyyy年MM月";
    /**
     * MM-dd
     */
    public static final String TYPE_MONTH_DAY = "MM-dd";
    /**
     * yyyy-MM-dd
     */
    public static final String TYPE_Date = "yyyy-MM-dd";
    /**
     * yyyy年MM月dd日
     */
    public static final String TYPE_Date_1 = "yyyy年MM月dd日";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String TYPE_Date_Time = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取（年月日）
     *
     * @param type :(TYPE_YEAR,TYPE_MONTH,TYPE_DAY)
     */

    public static int getDateInfo(String type, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        switch (type) {
            case TYPE_YEAR:
                return cal.get(Calendar.YEAR);
            case TYPE_MONTH:
                return cal.get(Calendar.MONTH) + 1;
            case TYPE_DAY:
                return cal.get(Calendar.DATE);
        }
        return 0;
    }

    /**
     * @param type :(TYPE_YEAR,TYPE_MONTH,TYPE_DAY)
     */
    public static Date getDiffDate(String type, Date date, int diff) {
        if (diff != 0) {
            long dateToTimestamp = date.getTime();
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            String dateStr = DateToString(TYPE_Date_Time, date);
            String temDateStr;
            int year = 0;
            int month = 0;
            int day = cal.get(Calendar.DAY_OF_MONTH);
            switch (type) {
                case TYPE_YEAR:
                    year = cal.get(Calendar.YEAR) + diff;
                    temDateStr = dateStr.substring(4, dateStr.length());
                    temDateStr = new StringBuilder().append(year).append(temDateStr).toString();
                    return StringToDate(TYPE_Date_Time, temDateStr);
                case TYPE_MONTH:
                    year = cal.get(Calendar.YEAR) + diff / 12;
                    month = cal.get(Calendar.MONTH) + 1;
                    int monthDiff = diff % 12;
                    if (monthDiff > 0) {
                        if (month + monthDiff > 12) {
                            year++;
                            month = month + monthDiff - 12;
                        } else {
                            month += monthDiff;
                        }
                    } else if (monthDiff < 0) {
                        if (month + monthDiff < 0) {
                            year--;
                            month = 12 + month + monthDiff;
                        } else {
                            month += monthDiff;
                        }
                    }
                    temDateStr = dateStr.substring(7, dateStr.length());
                    temDateStr = new StringBuilder().append(year).append("-").append(month).append(temDateStr).toString();
                    return StringToDate(TYPE_Date_Time, temDateStr);
                case TYPE_DAY:
                    long diffTimestamp = 24 * 60 * 60 * 1000 * diff;
                    long temTimestamp = dateToTimestamp + diffTimestamp;
                    return TimestampToDate(temTimestamp);
            }
        }
        return date;
    }

    public static SimpleDateFormat getDateFormat(String formatRule) {
        return new SimpleDateFormat(formatRule, Locale.CHINA);
    }

    public static String DateToString(String formatRule, Date date) {
        if (date == null) {
            date = new Date();
        }
        return getDateFormat(formatRule).format(date);
    }

    public static Date StringToDate(String formatRule, String dateStr) {
        try {
            return getDateFormat(formatRule).parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 时间戳转日期
     */
    public static Date TimestampToDate(long timestamp) {
        return new Date(timestamp);
    }

    /**
     * 时间戳转日期字符串
     */
    public static String TimestampToDateString(String formatRule, long timestamp) {
        return DateToString(formatRule, new Date(timestamp));
    }
}
